package com.kevis.zhunblog.admin.controller.modules.sys;

import com.github.pagehelper.PageInfo;
import com.kevis.webCommon.constant.ControllerConstant;
import com.kevis.webCommon.web.domain.response.Result;
import com.kevis.webCommon.web.domain.response.module.ResultTable;
import com.kevis.zhunblog.admin.controller.base.BaseController;
import com.kevis.zhunblog.core.dto.PageDomain;
import com.kevis.zhunblog.core.entities.AdminEntity;
import com.kevis.zhunblog.service.AdminService;
import com.kevis.zhunblog.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.UUID;

import static com.kevis.webCommon.web.domain.response.Result.decide;

@RestController
@Api(tags = {"用户管理"})
@RequestMapping(ControllerConstant.API_SYSTEM_PREFIX + "user")
public class SysUserController extends BaseController {
    /**
     * Describe: 基础路径
     */
    private static String MODULE_PATH = "system/user/";

    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleService roleService;

    /**
     * Describe: 密码加密
     * */
    @Autowired
    private PasswordEncoder passwordEncoder;

    @GetMapping("main")
    @ApiOperation("管理员列表视图")
    public ModelAndView main() {
        return jumpPage(MODULE_PATH + "main");
    }

    /**
     * Describe: 获取角色列表数据
     * Param SysRole PageDomain
     * Return 角色列表数据
     */
    @PostMapping("list")
    @ApiOperation(value = "获取用户列表数据")
    //@PreAuthorize("hasPermission('/system/role/data','sys:role:data')")
    public ResultTable data(PageDomain pageDomain, AdminEntity param) {
        PageInfo<AdminEntity> pageInfo = adminService.page(param, pageDomain);
        return pageTable(pageInfo.getList(), pageInfo.getTotal());
    }

    @PutMapping("enable")
    @ApiOperation(value = "状态更改")
    public Result setEnable(@RequestBody AdminEntity entity) {
        //public Result updateStatus(Integer userId, Integer enable) {
        return decide(adminService.updateById(entity));
    }

    @GetMapping("add")
    @ApiOperation("获取用户新增视图")
    public ModelAndView add(){
        return jumpPage(MODULE_PATH + "add");
    }

    /**
     * Describe: 用户新增接口
     * Param ModelAndView
     * Return 操作结果
     */
    //@Repeat  防止重复提交
    @PostMapping("add")
    @ApiOperation(value = "保存用户数据")
    //@PreAuthorize("hasPermission('/system/user/add','sys:user:add')")
    //@Log(title = "新增用户", describe = "新增用户", type = BusinessType.ADD)
    public Result save(@RequestBody AdminEntity sysUser) {
        //sysUser.setLogin("0");
        //sysUser.setEnable("1");
        sysUser.setGuid(UUID.randomUUID().toString());
        sysUser.setStatus((byte)0);
        sysUser.setAdminPassword(passwordEncoder.encode(sysUser.getAdminPassword()));
        adminService.save(sysUser);
        //adminService.saveUserRole(sysUser.getUserId(), Arrays.asList(sysUser.getRoleIds().split(",")));
        return Result.success("保存成功");
    }

    /**
     * Describe: 用户修改视图
     * Param ModelAndView
     * Return 返回用户修改视图
     */
    @GetMapping("edit")
    @ApiOperation(value = "获取用户修改视图")
    //@PreAuthorize("hasPermission('/system/user/edit','sys:user:edit')")
    public ModelAndView edit(Model model, String id) {
        model.addAttribute("sysRoles", roleService.list());
        model.addAttribute("sysUser", adminService.getById(id));
        return jumpPage(MODULE_PATH + "edit");
    }

    /**
     * Describe: 用户修改接口
     * Param ModelAndView
     * Return 返回用户修改接口
     */
    @PutMapping("edit")
    @ApiOperation(value = "修改用户数据")
    //@PreAuthorize("hasPermission('/system/user/edit','sys:user:edit')")
    //@Log(title = "修改用户", describe = "修改用户", type = BusinessType.EDIT)
    public Result update(@RequestBody AdminEntity sysUser) {
        //sysUserService.saveUserRole(sysUser.getUserId(), Arrays.asList(sysUser.getRoleIds().split(",")));
        adminService.updateById(sysUser);
        return Result.success("修改成功");
    }

    /**
     * Describe: 用户删除接口
     * Param: id
     * Return: Result
     */
    @Transactional(rollbackFor = Exception.class)
    @DeleteMapping("remove")
    @ApiOperation(value = "删除用户数据")
    //@PreAuthorize("hasPermission('/system/user/remove','sys:user:remove')")
    //@Log(title = "删除用户", describe = "删除用户", type = BusinessType.REMOVE)
    public Result remove(Integer id) {
        AdminEntity adminEntity = adminService.getById(id);
        adminEntity.setStatus((byte)-1);
        return decide(adminService.updateById(adminEntity));
        //return decide(adminService.removeById(id));
    }

    /**
     * Describe: 用户密码修改视图
     * Param ModelAndView
     * Return 返回用户密码修改视图
     */
    @GetMapping("editpasswordadmin")
    @ApiOperation(value = "获取管理员修改用户密码视图")
    //@PreAuthorize("hasPermission('/system/user/editPasswordAdmin','sys:user:editPasswordAdmin')")
    public ModelAndView editPasswordAdminView(Model model, Integer id) {
        model.addAttribute("id", id);
        return jumpPage(MODULE_PATH + "editPasswordAdmin");
    }

    /**
     * Describe: 管理员修改用户密码接口
     * Param editPassword
     * Return: Result
     */
    @PutMapping("editPasswordAdmin")
    @ApiOperation(value = "管理员修改用户密码")
    //@PreAuthorize("hasPermission('/system/user/editPasswordAdmin','sys:user:editPasswordAdmin')")
    public Result editPasswordAdmin(@RequestBody AdminEntity sysUser) {
        sysUser.setAdminPassword(passwordEncoder.encode(sysUser.getAdminPassword()));
        return decide(adminService.updateById(sysUser), "修改成功", "修改失败");
    }
}
